package com.enba.mallapi.module.orders.order.controller;

import com.enba.boot.core.base.Result;
import com.enba.mall.core.orders.order.req.OrderCancelReq;
import com.enba.mall.core.orders.order.req.OrderCommentOrderReq;
import com.enba.mall.core.orders.order.req.OrderCreateReq;
import com.enba.mall.core.orders.order.req.OrderRefundReq;
import com.enba.mall.core.orders.order.resp.MyOrderResp;
import com.enba.mall.core.orders.order.resp.OrderCreateResp;
import com.enba.mall.core.orders.order.service.IOrderService;
import com.enba.mall.core.orders.order.service.impl.PaymentService.CallbackResult;
import com.enba.mall.core.orders.order.resp.OrderDetailResp;
import com.enba.mallapi.annotation.CurrentUser;
import com.enba.mallapi.annotation.DistributedLock;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单主表
 *
 * @author 恩爸编程
 * @since 2025-05-26
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/order")
public class OrderController {

  @Resource private IOrderService orderService;

  /*
   购物车 → 【点击“立即下单/立即购买】” → 【确认收货地址（修改为“公司地址”）】→【 选择“满199减20”优惠券 】→ 点击“【立即支付（生成待支付单）】 → 进入支付页 → 点击“【确认付款】” → 完成付款
  */

  @PostMapping("/create")
  @ApiOperation("立即支付")
  @DistributedLock(tips = "已提交订单，请勿重复操作")
  public Result<OrderCreateResp> createOrder(
      @RequestBody @Validated OrderCreateReq orderCreateReq, @CurrentUser Long userId) {
    return Result.success(orderService.createOrder(orderCreateReq, userId));
  }

  @PostMapping("/pay-callback")
  @ApiOperation("支付回调")
  public ResponseEntity<CallbackResult> payCallback(HttpServletRequest request) throws IOException {
    return orderService.payCallback(request);
  }

  @PostMapping("/cancel")
  @ApiOperation("取消订单")
  @DistributedLock(tips = "取消订单中，请勿重复操作")
  public Result<Boolean> cancelOrder(
      @RequestBody @Validated OrderCancelReq req, @CurrentUser Long userId) {
    orderService.cancelOrder(req, userId);
    return Result.success(true);
  }

  @PostMapping("/close")
  @ApiOperation("关闭订单")
  @DistributedLock(tips = "关闭订单中，请勿重复操作")
  public Result<Boolean> closeOrder(@RequestParam Long orderId, @CurrentUser Long userId) {
    orderService.closeOrder(orderId, userId);
    return Result.success(true);
  }

  @PostMapping("/refund")
  @ApiOperation("用户申请退款")
  @DistributedLock(tips = "退款中，请勿重复操作")
  public Result<Boolean> refundOrder(@RequestBody OrderRefundReq req, @CurrentUser Long userId) {
    orderService.refundOrder(req, userId);
    return Result.success(true);
  }

  @PostMapping("/receive/{orderId}")
  @ApiOperation("用户确认收货")
  @DistributedLock(tips = "收货中，请勿重复操作")
  public Result<Boolean> receiveOrder(
      @PathVariable @NotNull(message = "订单ID不能为空") Long orderId, @CurrentUser Long userId) {
    orderService.receiveOrder(orderId, userId);
    return Result.success(true);
  }

  @PostMapping("/comment")
  @ApiOperation("评价")
  @DistributedLock(tips = "评价中，请勿重复操作")
  public Result<Boolean> commentOrder(
      @RequestBody @Validated OrderCommentOrderReq req, @CurrentUser Long userId) {
    orderService.commentOrder(req, userId);
    return Result.success(true);
  }

  @GetMapping("/my/{status}")
  @ApiOperation("我的订单列表")
  public Result<List<MyOrderResp>> myOrder(
      @PathVariable @ApiParam(value = "订单状态") String status, @CurrentUser Long userId) {
    return Result.success(orderService.findMyOrder(status, userId));
  }

  @GetMapping("/detail/{orderId}")
  @ApiOperation("我的订单详情")
  public Result<OrderDetailResp> orderDetail(
      @PathVariable @NotNull(message = "订单ID不能为空") Long orderId, @CurrentUser Long userId) {
    return Result.success(orderService.getOrderDetail(orderId, userId));
  }
}
